package cn.iocoder.yudao.module.trade.controller.app.delivery;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.trade.api.storeusers.StoreUsersApi;
import cn.iocoder.yudao.module.trade.api.storeusers.dto.StoreUsersRespDTO;
import cn.iocoder.yudao.module.trade.controller.app.delivery.vo.pickup.AppDeliveryPickUpStoreRespVO;
import cn.iocoder.yudao.module.trade.convert.delivery.DeliveryPickUpStoreConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryPickUpStoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 自提门店")
@RestController
@RequestMapping("/trade/delivery/pick-up-store")
@Validated
public class AppDeliverPickUpStoreController {

    @Resource
    private DeliveryPickUpStoreService deliveryPickUpStoreService;
    @Resource
    private StoreUsersApi storeUsersApi;
    @Resource
    private MemberUserApi memberUserApi;

    @GetMapping("/list")
    @Operation(summary = "获得自提门店列表")
    @Parameters({
            @Parameter(name = "latitude", description = "精度", example = "110"),
            @Parameter(name = "longitude", description = "纬度", example = "120")
    })
    public CommonResult<List<AppDeliveryPickUpStoreRespVO>> getDeliveryPickUpStoreList(
            @RequestParam(value = "latitude", required = false) Double latitude,
            @RequestParam(value = "longitude", required = false) Double longitude) {
        List<DeliveryPickUpStoreDO> list = deliveryPickUpStoreService.getAppletShowDeliveryPickUpStoreListByStatus(
                CommonStatusEnum.ENABLE.getStatus());
        return success(DeliveryPickUpStoreConvert.INSTANCE.convertList(list, latitude, longitude));
    }

    @GetMapping("/get")
    @Operation(summary = "获得自提门店")
    @Parameter(name = "id", description = "门店编号")
    public CommonResult<AppDeliveryPickUpStoreRespVO> getOrder(@RequestParam("id") Long id) {
        DeliveryPickUpStoreDO store = deliveryPickUpStoreService.getDeliveryPickUpStore(id);
        return success(DeliveryPickUpStoreConvert.INSTANCE.convert03(store));
    }

    @GetMapping("/listUserPowerForB")
    @Operation(summary = "获得B端用户权限门店列表")
    @PreAuthenticated
    public CommonResult<List<AppDeliveryPickUpStoreRespVO>> listUserPowerForB() {
        Long userId = getLoginUserId();
        MemberUserRespDTO user = memberUserApi.getUser(userId);
        if (user != null && StringUtils.isNotBlank(user.getMobile())) {
            List<StoreUsersRespDTO> storeUsers = storeUsersApi.listEnabledStoreUsersByMobile(user.getMobile());
            List<DeliveryPickUpStoreDO> deliveryPickUpStoreList = deliveryPickUpStoreService.getDeliveryPickUpStoreList(
                    storeUsers.stream().map(StoreUsersRespDTO::getStoreId).collect(Collectors.toList()));
            deliveryPickUpStoreList.removeIf(e -> Objects.equals(e.getStatus(), CommonStatusEnum.DISABLE.getStatus()));
            List<AppDeliveryPickUpStoreRespVO> result = BeanUtil.copyToList(deliveryPickUpStoreList, AppDeliveryPickUpStoreRespVO.class);
            result.forEach(e -> e.setAreaName(AreaUtils.format(e.getAreaId())));
            return success(result);
        }
        return success(Lists.newArrayList());
    }

}
